package com.lagou.springexp;

import com.lagou.rpc.proxy.RpcProxy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.FactoryBean;

/**
 * @ClassName ServiceFactoryBean
 * @Description 实现了FactoryBean方法，并且实现了getObject方法
 * @Author playboy
 * @Date 2021/6/5 4:35 下午
 * @Version 1.0
 **/
public class ServiceFactoryBean<T> implements FactoryBean<T> {
	private static Logger log = LoggerFactory.getLogger(ServiceFactoryBean.class);

	private RpcProxy rpcProxy;

	private Class<T> serverInterface;

	public ServiceFactoryBean(Class<T> serverInterface) {
		this.serverInterface = serverInterface;
	}

	public void setRpcProxy(RpcProxy rpcProxy) {
		this.rpcProxy = rpcProxy;
	}

	/**
	 * 使用jdk动态代理生成当前接口的代理类，然后放入到spring的IOC容器中
	 *
	 * @return
	 * @throws Exception
	 */
	@Override
	public T getObject() throws Exception {
		return rpcProxy.getProxy(this.serverInterface);
	}

	@Override
	public Class<?> getObjectType() {
		return this.serverInterface;
	}
}
